package com.faner.fast.upms.controller;


import com.faner.fast.annotation.SysLog;
import com.faner.fast.upms.domain.vo.MenuVO;
import com.faner.fast.upms.util.FastSecurityUtils;
import com.faner.fast.util.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import com.faner.fast.upms.model.SysMenuDO;
import com.faner.fast.upms.service.SysMenuService;
import lombok.RequiredArgsConstructor;

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author faner
 * @since 2021-01-26
 */
@RestController
@RequiredArgsConstructor
@Api(value = "menu", tags = "系统-菜单管理模块")
@ApiSupport(order = 50)
public class MenuController {

    private final SysMenuService sysMenuService;

    /**
     * 返回当前用户的树形菜单集合
     * @param parentId 父节点ID
     * @return 当前用户的树形菜单
     */
    @ApiOperation(value = "当前用户的树形菜单列表")
    @ApiOperationSupport(order = 10,author = "faner")
    @GetMapping("/menu/user")
    public R getUserMenu(Long parentId) {

        // 获取符合条件的菜单
        Set<MenuVO> all = new HashSet<>();
        FastSecurityUtils.getRoles().forEach(roleId -> all.addAll(sysMenuService.findMenuByRoleId(roleId)));
        return R.ok(sysMenuService.filterMenu(all, parentId));
    }

    /**
     * 返回树形菜单集合
     * @param lazy 是否是懒加载
     * @param parentId 父节点ID
     * @return 树形菜单
     */
    @ApiOperation(value = "树形菜单列表")
    @ApiOperationSupport(order = 20,author = "faner")
    @GetMapping(value = "/menu/tree")
    public R getTree(boolean lazy, Long parentId) {
        return R.ok(sysMenuService.treeMenu(lazy, parentId));
    }

    /**
     * 返回角色的菜单集合
     * @param roleId 角色ID
     * @return 属性集合
     */
    @ApiOperation(value = "角色的菜单列表")
    @ApiOperationSupport(order = 30,author = "faner")
    @GetMapping("/menu/tree/role")
    public R getRoleTree(@RequestParam Long roleId) {
        return R.ok(
                sysMenuService.findMenuByRoleId(roleId).stream().map(MenuVO::getId).collect(Collectors.toList()));
    }

    /**
     * 通过ID查询菜单的详细信息
     * @param id 菜单ID
     * @return 菜单详细信息
     */
    @ApiOperation(value = "菜单的详情")
    @ApiOperationSupport(order = 40,author = "faner")
    @GetMapping("/menu/detail")
    public R getById(@RequestParam Long id) {
        return R.ok(sysMenuService.getById(id));
    }

    /**
     * 新增菜单
     * @param sysMenu 菜单信息
     * @return 含ID 菜单信息
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_add')")
    @SysLog("新增菜单")
    @ApiOperation(value = "新增菜单")
    @ApiOperationSupport(order = 50,author = "faner")
    @PostMapping("/menu/save")
    public R save(@Validated @RequestBody SysMenuDO sysMenu) {
        sysMenuService.save(sysMenu);
        return R.ok(sysMenu);
    }

    /**
     * 更新菜单
     * @param sysMenu
     * @return
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_edit')")
    @SysLog("更新菜单")
    @ApiOperation(value = "更新菜单")
    @ApiOperationSupport(order = 60,author = "faner")
    @PostMapping("/menu/update")
    public R update(@Validated @RequestBody SysMenuDO sysMenu) {
        return R.ok(sysMenuService.updateMenuById(sysMenu));
    }

    /**
     * 删除菜单
     * @param id 菜单ID
     * @return success/false
     */
    @PreAuthorize("@pms.hasPermission('sys_menu_del')")
    @SysLog("删除菜单")
    @ApiOperation(value = "删除菜单")
    @ApiOperationSupport(order = 70,author = "faner")
    @PostMapping("/menu/delete")
    public R removeById(@RequestParam Long id) {
        return sysMenuService.removeMenuById(id);
    }



}

